package com.songcha.module_bookreader.ui.fragment.slide

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_business.bean.book.BookBean
import com.songcha.library_business.bean.book.BookChapterCatalogBean
import com.songcha.library_business.enums.BookDownloadStatus
import com.songcha.library_business.event.book.InBookShelfEvent
import com.songcha.library_business.proxy.database.BookDownloadDaoManager
import com.songcha.library_business.proxy.database.BookShelfDaoManager
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_network.bean.BaseBean
import org.greenrobot.eventbus.EventBus

class BookReadSlideViewModel(app: Application):BaseViewModel<BookReadSlideRepository>(app) {
    var book=BookBean()
    var chapterList= mutableListOf<BookChapterCatalogBean.DataBean>()
    //是否倒序
    var isReverse=false
    var isDark=false
    var bgIdx=1
    var isInBookShelf= MutableLiveData<Boolean>()
    var isBookAllDownload=MutableLiveData<Boolean>()
    var bookDownloadStatus= MutableLiveData(BookDownloadStatus.NO_DOWNLOAD)

    fun addBookToShelf(){
        if(BookShelfDaoManager.addToBookShelf(book.id)){
            isInBookShelf.value=true
            EventBus.getDefault().post(InBookShelfEvent(book.id,true))
        }
        if(UserInfoManager.checkLogin()){
            handleApiDataObserver(repository!!.addToBookShelf(book.id.toInt()),object :
                IApiResult<BaseBean> {
                override fun onSuccess(bean: BaseBean) {

                }
            })
        }
    }

    fun getIsInBookShelf(){
        isInBookShelf.value=BookShelfDaoManager.isInBookShelf(book.id)
    }

    fun setIsInBookShelf(isIn:Boolean){
        isInBookShelf.value=isIn
    }

    fun getHasBookAllDownload(){
        isBookAllDownload.value= BookDownloadDaoManager.hasBookAllDownload(book.id,chapterList.size)
    }


}